package com.xian;

import com.alibaba.fastjson.JSON;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MyTest {
    public static void main(String[] args) {

        /*
        部署路径：/data/apollo
        APP连接apollo-configservice的参数：
            env=dev
            dev.meta=http://172.18.6.1:6000
        Apollo WEB页面连接参数：
            http://172.18.6.1:8080
            user：devuser
            pass：devuser@wsecar
         */

        System.setProperty("env", "DEV");
        System.setProperty("app.id", "orderService");
        //指定apollo缓存目录
        //System.setProperty("apollo.cacheDir", "E:/data/apollo-cache-dir");
        System.setProperty("apollo.cacheDir", "/root/test/apollo-cache-dir");
        //指定连接地址
        System.setProperty("apollo.meta", "http://172.18.6.1:6000");
        //System.setProperty("apollo.meta", "http://172.18.6.1:8080");


        Config config = ConfigService.getConfig("wsShunFengChe.mongo"); //namepace=wsShunFengChe.mongo

        String value = config.getProperty("mongo.db.address", "nothing");
        System.out.println(value);

    }

    @Test
    public void test1(){
        System.out.println("纯java Api 获取apollo配置信息");

        /*
        部署路径：/data/apollo
        APP连接apollo-configservice的参数：
            env=dev
            dev.meta=http://172.18.6.1:6000
        Apollo WEB页面连接参数：
            http://172.18.6.1:8080
            user：devuser
            pass：devuser@wsecar
         */

        System.setProperty("env", "DEV");
        System.setProperty("app.id", "orderService");
        //指定apollo缓存目录
        //System.setProperty("apollo.cacheDir", "E:/data/apollo-cache-dir");
        System.setProperty("apollo.cacheDir", "/root/test/apollo-cache-dir");
        //指定连接地址
        System.setProperty("apollo.meta", "http://172.18.6.1:6000");
        //System.setProperty("apollo.meta", "http://172.18.6.1:8080");


        Config config = ConfigService.getConfig("wsShunFengChe.mongo"); //namepace=wsShunFengChe.mongo

        String value = config.getProperty("mongo.db.address", "nothing");
        System.out.println(value);

        //获取所有key
        System.out.println("===============");
        Set<String> keys = config.getPropertyNames();
        for (String key : keys) {
            System.out.println(key + "==" + config.getProperty(key, ""));
        }



    }

    //获取默认的namespace
    @Test
    public void test(){
        /*
        部署路径：/data/apollo
        APP连接apollo-configservice的参数：
            env=dev
            dev.meta=http://172.18.6.1:6000
        Apollo WEB页面连接参数：
            http://172.18.6.1:8080
            user：devuser
            pass：devuser@wsecar
         */

        System.setProperty("env", "DEV");
        System.setProperty("app.id", "orderService");
        //指定apollo缓存目录
        System.setProperty("apollo.cacheDir", "E:/data/apollo-cache-dir");
        //指定连接地址
        System.setProperty("apollo.meta", "http://172.18.6.1:6000");
        //System.setProperty("apollo.meta", "http://172.18.6.1:8080");


        Config config = ConfigService.getAppConfig(); //namepace=application

        String value = config.getProperty("im.hostname", "nothing");
        System.out.println(value);

    }


    //获取指定namespace
    @Test
    public void test2(){
        /*
        部署路径：/data/apollo
        APP连接apollo-configservice的参数：
            env=dev
            dev.meta=http://172.18.6.1:6000
        Apollo WEB页面连接参数：
            http://172.18.6.1:8080
            user：devuser
            pass：devuser@wsecar
         */

        System.setProperty("env", "DEV");
        System.setProperty("app.id", "orderService");
        //指定apollo缓存目录
        System.setProperty("apollo.cacheDir", "E:/data/apollo-cache-dir");
        //指定连接地址
        System.setProperty("apollo.meta", "http://172.18.6.1:6000");
        //System.setProperty("apollo.meta", "http://172.18.6.1:8080");


        Config config = ConfigService.getConfig("wsShunFengChe.mongo"); //namepace=wsShunFengChe.mongo

        String value = config.getProperty("mongo.db.dbname", "nothing");
        System.out.println(value);

    }


    //获取指定namespace,但不存在的key（该key存在于父公共配置中）
    @Test
    public void test3(){
        /*
        部署路径：/data/apollo
        APP连接apollo-configservice的参数：
            env=dev
            dev.meta=http://172.18.6.1:6000
        Apollo WEB页面连接参数：
            http://172.18.6.1:8080
            user：devuser
            pass：devuser@wsecar
         */

        System.setProperty("env", "DEV");
        System.setProperty("app.id", "orderService");
        //指定apollo缓存目录
        System.setProperty("apollo.cacheDir", "E:/data/apollo-cache-dir");
        //指定连接地址
        System.setProperty("apollo.meta", "http://172.18.6.1:6000");
        //System.setProperty("apollo.meta", "http://172.18.6.1:8080");


        Config config = ConfigService.getConfig("wsShunFengChe.mongo"); //namepace=wsShunFengChe.mongo

        String value = config.getProperty("mongo.db.address", "nothing");
        System.out.println(value);

    }

    @Test
    public void test4(){
        System.out.println("纯java Api 获取apollo配置信息");

        /*
        部署路径：/data/apollo
        APP连接apollo-configservice的参数：
            env=dev
            dev.meta=http://172.18.6.1:6000
        Apollo WEB页面连接参数：
            http://172.18.6.1:8080
            user：devuser
            pass：devuser@wsecar
         */

        System.setProperty("env", "DEV");
        System.setProperty("app.id", "orderService");
        //指定apollo缓存目录
        //System.setProperty("apollo.cacheDir", "E:/data/apollo-cache-dir");
        System.setProperty("apollo.cacheDir", "/root/test/apollo-cache-dir");
        //指定连接地址
        System.setProperty("apollo.meta", "http://172.18.6.1:6000");
        //System.setProperty("apollo.meta", "http://172.18.6.1:8080");


        Config config = ConfigService.getConfig("wsShunFengChe.mongo"); //namepace=wsShunFengChe.mongo

        //获取所有key
        System.out.println("===============");
        Map<String, String> map = new HashMap<>(); //存放kv值
        Set<String> keys = config.getPropertyNames();
        for (String key : keys) {
            String value = config.getProperty(key, "");
            map.put(key, value);
        }


        String resultJson = JSON.toJSONString(map);
        System.out.println(resultJson);

    }




}
